package com.wrupple.muba.catalogs.client.widgets.editors.composite;

import org.cobogw.gwt.user.client.CSS;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.CellTable.Resources;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimpleLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.wrupple.muba.catalogs.client.activity.process.task.CatalogInteractionState.Mode;
import com.wrupple.muba.catalogs.client.cms.service.CatalogManagementSystem;
import com.wrupple.muba.catalogs.client.module.services.logic.CatalogDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldConversionStrategyProvider;
import com.wrupple.muba.catalogs.client.module.services.logic.FieldDescriptionService;
import com.wrupple.muba.catalogs.client.module.services.presentation.GenericFieldFactory;
import com.wrupple.muba.catalogs.client.view.GenericCatalogEditor;
import com.wrupple.muba.catalogs.domain.CatalogEntry;
import com.wrupple.muba.common.shared.State.StorageManager;
import com.wrupple.vegetate.domain.FieldDescriptor;

/**
 * 
 * 
 * @author japi
 * 
 */
public class GenericCatalogEditorImpl extends CompositeCatalogEditor<CatalogEntry> implements GenericCatalogEditor {
	
	
	private ScrollPanel container;
	private FlexTable table;
	protected static final int DEFAULT_WIDTH = 600;
	
	
	
	@Inject
	public GenericCatalogEditorImpl(CatalogManagementSystem cms,StorageManager sm,GenericFieldFactory fieldFactory,
			FieldDescriptionService fieldService, CatalogDescriptionService catalogservice, FieldConversionStrategyProvider conversionService) {
		super( cms, sm, fieldService, catalogservice, conversionService, fieldFactory);
		container = new ScrollPanel();
		table = new FlexTable();
		CSS.setProperty(table, "marginLeft", "auto");
		CSS.setProperty(table, "marginRight", "auto");
		// set loading icon while the table is not initialized
		Resources r = GWT.create(CellTable.Resources.class);
		Image loading = new Image(r.cellTableLoading());
		container.setWidget(loading);
		SimpleLayoutPanel resizeWrapper = new SimpleLayoutPanel();
		resizeWrapper.add(container);
		initWidget(resizeWrapper);
	}

	@Override
	public HandlerRegistration addValueChangeHandler(String fieldId, ValueChangeHandler<?> handler) {
		HasValue widget = fields.get(fieldId);
		if (widget != null) {
			return widget.addValueChangeHandler(handler);
		}
		return null;
	}
	

	
	@Override
	public void setValue(CatalogEntry value) {
		table.removeAllRows();
		super.setValue(value);
	}
	@Override
	public void initialize(String catalog, Mode mode, EventBus bus) {
		super.initialize(catalog, mode, bus);
		container.setWidget(table);		
	}


	@Override
	protected void maybeAddField(HasValue<Object> field,
			FieldDescriptor fdescriptor) {
		int row = table.getRowCount();
		table.setWidget(row, 0, new Label(fdescriptor.getName()));
		table.setWidget(row, 1, (Widget)field );
	}
}
